package negocio;

import java.util.List;

import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import entidades.Book;

/**
 * Session Bean implementation class Books
 */
@Stateless
public class Books implements BooksLocal, BooksRemote {

	@PersistenceContext(unitName = "EJB Tutorial")
    private EntityManager entityManager;

    public void addBook(Book b) throws Exception {
        entityManager.persist(b);
    }

    public void deleteBook(Book b) throws Exception {
        entityManager.remove(b);
    }

    public List<Book> getBooks() throws Exception {
        Query query = entityManager.createQuery("SELECT b from Book as b");
        List<Book> books = (List <Book>)query.getResultList(); 
        return books;
    }
    
    public List<Book> getBooksByFernando() throws Exception {
    	Query q = entityManager.createNamedQuery("librosFernando");
    	return q.getResultList();
    }
}
